package com.himore.api.web.service.impl;

import java.util.List;

import com.himore.api.common.core.domain.entity.SysUser;
import com.himore.api.common.core.text.Convert;
import com.himore.api.common.utils.DateUtils;
import com.himore.api.common.utils.SecurityUtils;
import com.himore.api.web.mapper.TSysCommentMapper;
import com.himore.api.web.mapper.TSysInvitationMapper;
import com.himore.api.web.mapper.TSysLikeMapper;
import com.himore.api.web.model.TSysComment;
import com.himore.api.web.model.TSysInvitation;
import com.himore.api.web.model.TSysLike;
import com.himore.api.web.service.ITSysLikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 点赞信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-05-13
 */
@Service
public class TSysLikeServiceImpl implements ITSysLikeService
{
    @Autowired
    private TSysLikeMapper tSysLikeMapper;


    @Autowired
    private TSysInvitationMapper tSysInvitationMapper;


    @Autowired
    private TSysCommentMapper tSysCommentMapper;
    /**
     * 查询点赞信息
     * 
     * @param likeId 点赞信息ID
     * @return 点赞信息
     */
    @Override
    public TSysLike selectTSysLikeById(Long likeId)
    {
        return tSysLikeMapper.selectTSysLikeById(likeId);
    }

    /**
     * 查询点赞信息列表
     * 
     * @param tSysLike 点赞信息
     * @return 点赞信息
     */
    @Override
    public List<TSysLike> selectTSysLikeList(TSysLike tSysLike)
    {
        return tSysLikeMapper.selectTSysLikeList(tSysLike);
    }

    /**
     * 新增点赞信息
     * 
     * @param tSysLike 点赞信息
     * @return 结果
     */
    @Override
    public int insertTSysLike(TSysLike tSysLike)
    {
        tSysLike.setCreateTime(DateUtils.getNowDate());
        SysUser user= SecurityUtils.getLoginUser().getUser();
        tSysLike.setUserId(user.getUserId());
        if("1".equals(tSysLike.getLikeType())){
            TSysInvitation invitation = new TSysInvitation();
            invitation.setInvitationId(tSysLike.getInvitationId());
            invitation.setNumberOfLikes(1);
            tSysInvitationMapper.updateCommentCount(invitation);
        }
        if("2".equals(tSysLike.getLikeType())){
            TSysComment tSysComment = new TSysComment();
            tSysComment.setCommentId(tSysLike.getCommentId());
            tSysComment.setInvitationId(tSysLike.getInvitationId());
            tSysComment.setNumberOfLikes(1L);
            tSysCommentMapper.updateLikeCount(tSysComment);
        }
        return tSysLikeMapper.insertTSysLike(tSysLike);
    }

    /**
     * 修改点赞信息
     * 
     * @param tSysLike 点赞信息
     * @return 结果
     */
    @Override
    public int updateTSysLike(TSysLike tSysLike)
    {
        tSysLike.setUpdateTime(DateUtils.getNowDate());
        return tSysLikeMapper.updateTSysLike(tSysLike);
    }

    /**
     * 删除点赞信息对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteTSysLikeByIds(String ids)
    {
        return tSysLikeMapper.deleteTSysLikeByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除点赞信息信息
     * 
     * @param likeId 点赞信息ID
     * @return 结果
     */
    @Override
    public int deleteTSysLikeById(Long likeId)
    {
        return tSysLikeMapper.deleteTSysLikeById(likeId);
    }

    @Override
    public int deleteTSysLikeByUserId(TSysLike tSysLike) {
        if("1".equals(tSysLike.getLikeType())){
            TSysInvitation invitation = new TSysInvitation();
            invitation.setInvitationId(tSysLike.getInvitationId());
            invitation.setNumberOfLikes(-1);
            tSysInvitationMapper.updateCommentCount(invitation);
        }
        if("2".equals(tSysLike.getLikeType())){
            TSysComment tSysComment = new TSysComment();
            tSysComment.setCommentId(tSysLike.getCommentId());
            tSysComment.setNumberOfLikes(-1L);
            tSysCommentMapper.updateLikeCount(tSysComment);
        }
        return tSysLikeMapper.deleteTSysLikeByUserId(tSysLike);
    }
}
